﻿using System;
using System.Data;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.DAL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class Login : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_HRM_PS_Employees emp = new B_HRM_PS_Employees();
        IBLL_FIM_SystemControls sc = new B_FIM_SystemControls();
        D_SECURITY_Users user = new D_SECURITY_Users();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["loginadminforme"] == "true") //  need to be remove when deploy
            {
                hHeight.Value = "700";
                CheckPassword("administrator", "p@ssw0rd");
                Response.Redirect(@"~\App_UI\Forms\FIM_Payments.aspx");
            }
        }

        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            string strUserID = txtUserID.Text.ToUpper().Trim();
            string strPassword = txtPassword.Text.Trim();

            //============= Test Area ==============
            //if (strUserID.Length == 0)
            //{
            //    strUserID = "70";
            //    strPassword = "kpis";
            //}
            //======================================

            if (strUserID.Length == 0)
            {
                lblError.Text = "กรุณากรอก [รหัสประจำตัว]";
                return;
            }

            if (!CheckPassword(strUserID, strPassword))
            {
                return;
            }

            //main.MessageAlert(Page, mr.GetNextRefNo("2555", "1"));

            Response.Redirect(@"~\App_UI\Forms\Default.aspx");
        }

        private M_FIM_SystemControls GetSystemControl()
        {
            M_FIM_SystemControls info = sc.GetInfo();

            return info;
        }

        private bool CheckPassword(string strUserID, string strPassword)
        {
            bool bOK = false;
            DataTable dtb = user.GetUserInfo(strUserID, strPassword);
            string strCorrectPassword = "";

            if (dtb.Rows.Count == 0)
            {
                lblError.Text = "ไม่พบ [รหัสประจำตัว] นี้";
                return bOK;
            }
            else
            {
                strCorrectPassword = dtb.Rows[0]["user_password"].ToString();
            }

            if (strPassword == strCorrectPassword)
            {
                Session["EmpSeq"] = dtb.Rows[0]["emp_seq"].ToString();
                Session["UserJobsiteSeq"] = dtb.Rows[0]["working_job_site_seq"].ToString();
                //Session["SystemJobsiteSeq"] = "2983";
                //Session["SystemJobsiteUpperSeq"] = "2898";

                Session["UserInternalJobsiteSeq"] = dtb.Rows[0]["working_job_site_seq"].ToString();
                //Session["SystemInternalJobsiteSeq"] = "1300902983";
                //Session["SystemInternalJobsiteUpperSeq"] = "2898";
                Session["UserName"] = dtb.Rows[0]["user_name"].ToString();
                Session["UserWorkAreaSeq"] = dtb.Rows[0]["work_area_seq"].ToString();
                Session["Height"] = hHeight.Value;
                Session["SCInfo"] = GetSystemControl();
                new System.Web.UI.SsMng().SetLoginSession(dtb.Rows[0]["user_seq"].ToString(),
                    dtb.Rows[0]["emp_seq"].ToString(),
                    dtb.Rows[0]["user_name"].ToString(),
                    dtb.Rows[0]["working_job_site_seq"].ToString(),
                    hHeight.Value.ToString(),
                    null, null);
                bOK = true;
            }
            else
            {
                lblError.Text = "[รหัสผ่าน] ไม่ถูกต้อง";
            }

            return bOK;
        }
    }
}
